package com.task.core.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.task.common.redis.RedisKeyConstants;
import com.task.core.vo.resp.FlowFilterDetailRespVo;
import org.springframework.stereotype.Service;

import com.task.common.annotation.DataScope;
import com.task.common.core.redis.RedisCache;
import com.task.common.domain.info.KeyAndValue;
import com.task.common.exception.ServiceException;
import com.task.common.info.FlowFilterInfo;
import com.task.common.info.IpFraudInfo;
import com.task.common.utils.SecurityUtils;
import com.task.common.utils.StringUtils;
import com.task.common.utils.uuid.UUID;
import com.task.core.dto.request.FlowFilterRequestDTO;
import com.task.core.mapper.FlowFilterMapper;
import com.task.core.service.FlowFilterService;

/**
 * @author 迪迦.
 * @date 2024/11/4 17:26
 */
@Service("FlowFilterService")
public class FlowFilterServiceImpl implements FlowFilterService {

  @Resource
  private FlowFilterMapper flowFilterMapper;

  @Resource
  private RedisCache redisService;

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<FlowFilterInfo> getFlowFilterByPage(FlowFilterRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return flowFilterMapper.getFlowFilterByPage(request);
  }

  @Override
  //@DataScope(deptAlias = "dept", userAlias = "su")
  public List<KeyAndValue> getFlowFilterForOption(FlowFilterRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return flowFilterMapper.getFlowFilterForOption(request);
  }

  @Override
  public void addFlowFilter(FlowFilterRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    request.setCode(UUID.randomUUID().toString().replace("-", ""));
    flowFilterMapper.addFlowFilter(request, StringUtils.isEmpty(request.getPattern()) ? null : request.getPattern().getBytes());
    redisService.setCacheObject(RedisKeyConstants.FLOW_FILTER_INFO.formatKey(request.getCode()), new FlowFilterInfo(request.getId(),request.getCode(), request.getFilterName(),
        request.getType(), request.getPattern(), request.getDescription()));
  }

  @Override
  public void modifyFlowFilter(FlowFilterRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    Integer integer = flowFilterMapper.modifyFlowFilter(request, StringUtils.isEmpty(request.getPattern()) ? null : request.getPattern().getBytes());
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
    FlowFilterInfo flowFilter = flowFilterMapper.getFlowFilterByCode(request);
    redisService.setCacheObject(RedisKeyConstants.FLOW_FILTER_INFO.formatKey(request.getCode()), new FlowFilterInfo(flowFilter.getId(),request.getCode(), request.getFilterName(),
        request.getType(), request.getPattern(), request.getDescription()));
  }

  @Override
  public void deleteFlowFilter(String flowFilterCode, Long userId) {
    Integer integer = flowFilterMapper.deleteFlowFilter(flowFilterCode, userId);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
    //删除关联数据
    //删除和任务关联的数据
    flowFilterMapper.deleteMissionFlowFilterRelation(flowFilterCode);
    redisService.deleteObject(RedisKeyConstants.FLOW_FILTER_INFO.formatKey(flowFilterCode));
  }

  @Override
  //@DataScope(deptAlias = "dept", userAlias = "su")
  public List<FlowFilterInfo> getFlowFilterByPageInMissionDetail(FlowFilterRequestDTO request) {
    return flowFilterMapper.getFlowFilterByPageInMissionDetail(request);
  }

  @Override
  @DataScope(deptAlias = "dept",userAlias = "su")
  public FlowFilterDetailRespVo getFlowFilterDetail(String flowFilterCode) {
    FlowFilterRequestDTO flowFilterRequestDTO = new FlowFilterRequestDTO();
    flowFilterRequestDTO.setCode(flowFilterCode);
    return flowFilterMapper.getFlowFilterDetail(flowFilterRequestDTO);
  }
}
